package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clqteam.service.AccountService;
import com.clqteam.serviceimpl.AccountServiceImpl;

import net.sf.json.JSONObject;

/**
 * 关键字查询收支控制层
 * @author ZHX
 *
 */
public class SearchAccountController extends HttpServlet{
	
	AccountService aSer = new AccountServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF8");
		resp.setCharacterEncoding("UTF8");
		
		int page = Integer.parseInt(req.getParameter("page"));
		int rows = Integer.parseInt(req.getParameter("rows"));
		String begindt = req.getParameter("begindt"); 
		String enddt = req.getParameter("enddt");
		String key = req.getParameter("search");		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String date = sdf.format(d);
		String date1 = sdf1.format(d);
		if(begindt == ""){
			begindt = date1+" 00:00:00";
		}
		
		if(enddt == ""){
			enddt = date;
		}
			
		JSONObject j = aSer.findAccountByKeyAndDT(key, begindt, enddt, page, rows);
		
		PrintWriter pw = resp.getWriter();
		pw.write(j.toString());
		pw.flush();
		pw.close();		
		
	}

}
